home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1904 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  90 lines

  1. Path: fas.harvard.edu!berriz
  2. From: Zorro <berriz@husc.harvard.edu>
  3. Newsgroups: comp.lang.c
  4. Subject: Weird perl-C interaction (rand)
  5. Date: 17 Jan 1996 18:36:32 GMT
  6. Organization: Harvard University, Cambridge, Massachusetts
  7. Message-ID: <4djfjg$l45@decaxp.harvard.edu>
  8. Reply-To: berriz@husc.harvard.edu
  9. NNTP-Posting-Host: fas.harvard.edu
  10. Keywords: perl, C, rand, random
  11. X-Newsreader: NN version 6.5.0 #3 (NOV)
  12. Originator: berriz@fas.harvard.edu
  13.  
  14.  
  15.  
  16. I tried the following in a perl script to generate seeds to be given
  17. as arguments in calls to a C program:
  18.  
  19.   $seed = int(rand((2**31) -1)) + 1;
  20.  
  21. The C program stores this value in its own local variable "seed" and,
  22. of course, uses it to seed its random number generator:
  23.  
  24.   srandom(seed);
  25.  
  26. Then, the C program calls random(), and takes the modulo 32768 of the
  27. returned value.
  28.  
  29.   u = random() % 32768;
  30.  
  31. To my utter wilderment the resulting number u is *always* the same,
  32. even though the seeds produced by the perl script are always
  33. different.
  34.  
  35. I got around the problem by switching the perl code to
  36.  
  37.   $seed = int(rand((2**14) -1)) + 1;
  38.  
  39. ...but I'm clueless as to what's going on.  Does anybody know?  More
  40. to the point, is my "solution" OK?
  41.  
  42. FWIW, I'm running this on an IBM RS/6000, under AIX 3.2.5.  Below are
  43. toy versions of the perl script ("mumble") and the C program
  44. ("frotz.c", compiled to the executable "frotz"); they exhibit the
  45. problem described above.  I've also included mumble's output.
  46.  
  47. Thanks in advance,
  48.  
  49. Z.
  50.  
  51.  
  52. ... mumble ..................................................................
  53.  
  54. #!/usr/local/bin/perl -w
  55.  
  56. srand(time|$$);
  57.  
  58. for(1..10) {
  59.   $seed = int(rand((2**31) -1)) +1;
  60.   system("frotz $seed");
  61. }
  62.  
  63. ..........................................................................
  64.  
  65. /* frotz.c */
  66. void main(int argc, char *argv[]) {
  67.  
  68.   int x;
  69.   unsigned int seed;
  70.  
  71.   seed = (unsigned) atoi(argv[1]);
  72.   srandom(seed);
  73.   x = random();
  74.  
  75.   printf("%12d %12d %6d\n", seed, x, x % 32768);
  76. }
  77.  
  78. ..........................................................................
  79. $ mumble
  80.    494862336   1155565115   1595
  81.    614662144    884639291   1595
  82.    963575808   1387824699   1595
  83.   2010775552   1296303675   1595
  84.   1405616128   1142359611   1595
  85.     84934656    994772539   1595
  86.   1688469504    272434747   1595
  87.   1452736512   1738311227   1595
  88.    402128896   1452213819   1595
  89.   1499332608    789448251   1595
  90.